package com.dycn.service;

import com.dycn.dao.SysResourceMapper;
import com.dycn.dao.SysRoleMapper;
import com.dycn.dao.SysRoleResourceMapper;
import com.dycn.model.SysResource;
import com.dycn.model.SysRole;
import com.dycn.model.SysRoleResource;
import com.dycn.util.AppResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleService {

    @Autowired
    SysRoleMapper sysRoleMapper;
  @Autowired
  SysResourceMapper sysResourceMapper;
    @Autowired
    SysRoleResourceMapper sysRoleResourceMapper;

    public AppResultData list(int page){
        PageHelper.startPage(page,10);
        List<SysRole> sysRoleList=sysRoleMapper.list();
        PageInfo pageInfo=new PageInfo(sysRoleList);
        return  AppResultData.success(pageInfo.getSize()+"",pageInfo.getList());
    }

    public AppResultData findRoleVo(String roleId){
        return  AppResultData.success("成功",sysResourceMapper.findRoleVo(roleId));
    }

    public  AppResultData save(Integer roleId,String ids){
        sysRoleResourceMapper.deleteByRoleId(roleId);
        String idss[]=ids.split(",");
        for (String id:idss){
            SysRoleResource sysRoleResource=new SysRoleResource();
            SysResource sysRole = sysResourceMapper.selectByPrimaryKey(Integer.parseInt(id));
            sysRoleResource.setDelFlag(0);
            sysRoleResource.setResourceCode(sysRole.getResourceCode());
            sysRoleResource.setResourceId(sysRole.getId());
            sysRoleResource.setRoleId(roleId);
            sysRoleResourceMapper.insert(sysRoleResource);
        }
        return AppResultData.errorMessage("成功");
    }
}
